package com.epam.run;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

import com.epam.airlines.Aircraft;
import com.epam.airlines.Airlines;

public class SerializationTest {
	public static void serialize(Airlines airlines) {
		List<Aircraft> depot = airlines.getDepot();
		Aircraft aircraft1 = depot.get(0);
		Aircraft aircraft2 = depot.get(1);
		try {
			File file = new File(System.getProperty("user.dir")
					+ "\\src\\com\\epam\\aircrafts.dat");
			ObjectOutputStream oos = new ObjectOutputStream(
					new FileOutputStream(file));
			oos.writeObject(aircraft1);
			oos.writeObject(aircraft2);
			oos.close();
			FileInputStream is = new FileInputStream(file);
			ObjectInputStream ois = new ObjectInputStream(is);
			while (is.available() > 0) {
				Aircraft aircraft = (Aircraft) ois.readObject();
				System.out.println(aircraft.toString());
			}
			ois.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
